const Cc = Components.classes;
const Ci = Components.interfaces;
const Co = Components.Constructor;

Components.utils.import("resource://gsync/utils.js");
Components.utils.import("resource://gsync/gsync-api.js");

var settings = {

    passwordLocation: "chrome://gsync",
	
	passwordManager: Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager),
    
    onLoad: function(event){
    
        LOG.trace("settings-onLoad()");
        
        var loginData = this.getLoginData();
        
        if (loginData != null) {
            document.getElementById("txtEmail").value = loginData.email;
            document.getElementById("txtPassword").value = loginData.password;
        }
    },
	
	onLoginChanged: function(event) {
		
		LOG.trace("settings-onLoginChanged()");
	},
    
    onUploadBtnClicked: function(){
		
		LOG.trace("settings BEGIN onUploadBtnClicked()");
        g_SyncAPI.overrideRemote();
		LOG.trace("settings END onUploadBtnClicked()");
    },
    
    onDownloadBtnClicked: function(){
		
		LOG.trace("settings BEGIN onDownloadBtnClicked()");
		g_SyncAPI.overrideLocal();
		LOG.trace("settings END onDownloadBtnClicked()")
    },
    
    onSyncBtnClicked: function(){
		
        LOG.trace("settings BEGIN onSyncBtnClicked()");
        g_SyncAPI.synchronize();
		LOG.trace("settings END onSyncBtnClicked()");
    },
    
    
    onCancel: function(event){
    
        LOG.trace("settings-onCancel()");
        return true;
    },
    
    onAccept: function(event){
    
        LOG.trace("settings-onAccept()");
        var email = document.getElementById("txtEmail").value;
        var password = document.getElementById("txtPassword").value;
        if (email.length > 0 && password.length > 0) {
            this.updatePassword(email, password);
        }
        return true;
    },
    
    
    onShowLog: function(event){
        alert("TODO:Implement me");
    },
    
    updatePassword: function(email, password){
    
        var logins = this.passwordManager.findLogins({}, this.passwordLocation, "/", null);
        
        if (email && email.length > 0) {
        
            // Create or modify login information
            var nsLoginInfo = new Co("@mozilla.org/login-manager/loginInfo;1", Ci.nsILoginInfo);
            var loginInfo = new nsLoginInfo();
            loginInfo.init(this.passwordLocation, '/', null, email, password, '', '');
            
            if (logins.length < 1) {
                this.passwordManager.addLogin(loginInfo);
            } else {
                this.passwordManager.modifyLogin(logins[0], loginInfo);
            }
        } else {
            // Delete login information
            if (logins.length > 0) {
                this.passwordManager.removeLogin(logins[0]);
            }
        }
    },
    
    getLoginData: function(){
    
        var loginData = {
            email: null,
            user: null
        };
        var logins = this.passwordManager.findLogins({}, this.passwordLocation, "/", null);
        if (logins.length > 0) {
            loginData.email = logins[0].username;
            loginData.password = logins[0].password;
        }
        return loginData;
    },
}
